export default {
    data() {
        return {
            dicData: {}
        }
    },
    computed: {
    },
    methods: {
        getDataListByKey(key) {
            let data = this
            let keys = key.split(".")
            keys.forEach(k => {
                if (data) data = data[k]
            })
            return data || []
        },

        /**
         * 生命周期函数--监听页面加载
         */
        getDataLabelById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find(item => {
                    return id && item.id && item.id.toString() === id.toString()
                })
                if (find) return find.label
            }
            return ""
        },

        /**
         * 根据索引值获取对应id
         */
        getIdByIndex(key, index) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find((item, position) => {
                    return index === position
                })
                if (find) return find.id
            }
            return ''
        },

        /**
         * 根据索引值获取对应label
         */
        getLabelByIndex(key, index) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let find = dataList.find((item, position) => {
                    return index === position
                })
                if (find) return find.label
            }
            return ''
        },

        /**
         * 根据id获取对应value
         */
        getLabelById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let index = dataList.findIndex(item => {
                    return item.id && item.id.toString() === id.toString()
                })
                if (index > -1) return dataList[index].label
            }
            return ''
        },

        /**
         * 根据id值获取对应索引
         */
        getIndexById(key, id) {
            let dataList = this.getDataListByKey(key)
            if (dataList) {
                let index = dataList.findIndex(item => {
                    return item.id && id &&  item.id.toString() === id.toString()
                })
                if (index > -1) return index
            }
            return null
        },

        /**
         * 查询字典数据
         * @param keys
         * @returns {Promise<unknown>}
         */
        getDictionaryList (keys) {
            let self = this
            return new Promise(function (resolve, reject) {
                self.$api.getChildDictionaryListByKeys({
                    keys: keys
                }).then(res => {
                    let dicData = {}
                    keys.forEach(key => {
                        let dataList = res[key] || []
                        dataList.forEach(item => {
                            item.id = item.key
                            item.label = item.name
                            delete item.key
                            delete item.name
                        })
                        dicData[key] = dataList
                    })
                    self.dicData = dicData
                    resolve()
                }).catch(e => {
                    reject()
                })
            })
        }
    }
}
